<template>
	<div>
		<div class="addNum">
			<p class="addNum-title">添加账号</p>
			<div class="addNum-box">
				<span>账号</span>
				<el-input class="addNum-inp" v-model="userNum" placeholder="请输入账号" clearable></el-input>
			</div>
			<div class="addNum-box">
				<span>密码</span>
				<el-input class="addNum-inp" type="password" v-model="pw" placeholder="请输入密码" clearable></el-input>
			</div>
			<el-button type="primary" @click="addUser">添加</el-button>
		</div>
		<div class="userlist">
			<el-table border stripe ref="multipleTable" :data="tableData3" tooltip-effect="dark" style="width: 100%">
				<el-table-column type="selection" align="center" width="55">
				</el-table-column>
				<el-table-column prop="account_number" align="center" label="账号" width="200">
				</el-table-column>
				<el-table-column prop="create_time" align="center" label="注册时间" width="150" show-overflow-tooltip>
				</el-table-column>
				<el-table-column prop="last_time" align="center" label="最后登录时间" width="150" show-overflow-tooltip>
				</el-table-column>
				<el-table-column label="操作" align="center">
					<template slot-scope="scope">
						<el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				userNum: '',
				pw: '',
				tableData3: []
			}
		},
		methods: {
			addUser() {
				let ajaxData = {
					num: this.userNum,
					pw: this.pw
				};
				if(/\s/.test(ajaxData.num)) {
					this.$message.error('账号里不能存在空格');
					return;
				}
				if(/\s/.test(ajaxData.pw)) {
					this.$message.error('密码里不能存在空格');
					return;
				}

				this.axios.post('http://blog.cn/user/addUser', ajaxData)
					.then(response => {
						this.$notify({
							title: '提示',
							message: response.data.msg
						});
					})
					.catch(error => {
						console.log(error);
						alert('网络错误，不能访问');
					});
			},
			handleDelete(index, row) {
				this.$confirm('此操作将永久删除, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.axios.post('http://blog.cn/user/addUser', {
							id: row.id
						})
						.then(response => {
							this.$notify({
								title: '提示',
								message: response.data.msg
							});
						})
						.catch(error => {
							console.log(error);
							alert('网络错误，不能访问');
						});

				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});

			}
		},
		created() {
			this.axios.get('http://blog.cn/user/list')
				.then(response => {
					console.info(response);
					this.tableData3 = response.data.result;
				})
				.catch(error => {
					console.log(error);
					alert('网络错误，不能访问');
				});
		}
	}
</script>

<style scoped>
	.addNum-title {}
	
	.addNum-box {
		margin-bottom: 20px;
	}
	
	.addNum-inp {
		width: 200px;
	}
	
	.userlist {
		width: 1000px;
		margin: 0 auto;
	}
</style>